﻿using MediatR;
using PublicClass;
using S.Services.SheepMale.Domain.Select;
using S.Services.SheepMale.Infrastructure;
using S.Services.SheepMale.Read.Api.Application.ReadCommand.ReadSheepMaleCommand;

namespace S.Services.SheepMale.Read.Api.Application.ReadCommandHandler.ReadSheepMaleCommandHandler
{
    public class GetSheepTypeCommandHandler : IRequestHandler<GetSheepTypeCommand, List<SelectList>>
    {
        private readonly IRepository<SheepTypeInfo> repository;

        public GetSheepTypeCommandHandler(IRepository<SheepTypeInfo> repository)
        {
            this.repository = repository;
        }

        public async Task<List<SelectList>> Handle(GetSheepTypeCommand request, CancellationToken cancellationToken)
        {
            var r = await repository.AllAsync();
            var result=r.ToList();
            List<SelectList> lists = new List<SelectList>();
            foreach (var item in result)
            {
                lists.Add(new SelectList
                {
                    Id = item.SheepTypeId,
                    Name = item.SheepTypeName,
                });
            }
            return lists;
        }
    }
}
